<?php
/*
 * Created on 21 janv. 2009
 * Created by Ludovic Reenaers
 * lreenaers@hotmail.com
 */
class View
{
	private $doctype;
	private $head;
	private $title;
	private $content;
	private $footer;
	private $html;
	private $MenuTree;
	private $Model;
	private $header;

	private $pathway;
	private $languagePad;
	protected $translator;

	function __construct($Model,$Translator,$doctype="XHTML1.0T",$layout="BASE",$headertype="BASE")
	{
		$this->translator = $Translator;
		$this->Model =$Model;
		$this->doctype.=file_get_contents("html/encodings/".$doctype)."\n";
		$scripts = "";
		if(file_exists($_SERVER{'DOCUMENT_ROOT'}."/js/".$_SESSION["module"].".js")){
			$scripts.='<script type="text/javascript" src="/js/'.$_SESSION["module"].'.js"></script>'."\n";
		}
		if(file_exists($_SERVER{'DOCUMENT_ROOT'}."/js/common.js")){
			$scripts.='<script type="text/javascript" src="/js/common.js"></script>'."\n";
		}	
		if(file_exists($_SERVER{'DOCUMENT_ROOT'}."/css/".$_SESSION["module"].".css")){
			$scripts.='<link media="screen" rel="stylesheet" type="text/css" href="/css/'.$_SESSION["module"].'.css" />'."\n";
		}
		if(file_exists($_SERVER{'DOCUMENT_ROOT'}."/css/common.css")){
			$scripts.='<link media="screen" rel="stylesheet" type="text/css" href="/css/common.css" />'."\n";
		}		
			
		$this->head .= file_get_contents("html/headers/".$headertype)."\n".$scripts."\n";
		
		if (strtolower($_SESSION['module'])=='document'){
			$this->title = "<title>".ucfirst($_SESSION['docname'])."</title>\n";
		}elseif (strtolower($_SESSION['module'])== 'category') {
			$this->title = "<title>".ucfirst($_SESSION['catname'])."</title>\n";;
		}else{
			$this->title = "<title>".ucfirst($_SESSION['catname'])."</title>\n";
		}

		$this->html .= file_get_contents("html/templates/".$layout);
		$this->MenuTree=$this->Model->getMenuTree();
		$this->header='<h1 class="sitetitle"><a href="'.$_SESSION['config']->controler.'?/homepage/show/">'.$_SESSION['config']->sitetitle.'</a></h1>';
		$this->footer="";
		$this->languagePad = $this->Model->getLanguagePad();
		
		$this->pathway =$this->Model->getBreadCrumb();
		
		 

	}

	function setContent($str)
	{
		$this->content.=$str;
	}
	function redirect($url){
		$this->setContent('<META HTTP-EQUIV="Refresh" CONTENT="0; URL='.$url.'">');
	}
	function getPage()
	{
		$arrayBase=array("DOCTYPE","METAHEAD","TITLE","CONTENT","FOOTER","MENUG","HEADER","PATHWAY","LANGUAGES","USERPAD");
		$arrayView=array($this->doctype,$this->head,$this->title,$this->content,$this->footer,$this->MenuTree,$this->header,$this->pathway,$this->languagePad,$this->Model->getUserPad());
		$ret=str_replace($arrayBase,$arrayView,$this->html);
		return $ret;
	}

}
?>